/**
 *			\author ITII - ISEN - P6
 *			\author Jonathan CASSAING
 *			\file SmtpProtocol.cpp
 *			\version 0.3
 *			\date 09/09/2010
 */


#include <stdio.h>
#include <string.h>
#include <iostream>
#include "SmtpProtocol.h"


using namespace std;


/**
 * Warning! This function has been deprecated!
 */
int SmtpProtocol::formatMail( char * buffer,
			      int bufferSize,
			      const Mail & mail )
{
	string	mailFormated;
	string	heloStr(HELO);
	string	mailFromStr(MAIL_FROM);
	string	rcptToStr(RCPT_TO);
	string	dataStr(DATA);
	string	quitStr(QUIT);

	if( 0 >= bufferSize )
	{
		cerr << "Error: The value of the buffer size is " << bufferSize << " !" << endl;
		return -1;
	}
	if( NULL == buffer )
	{
		cerr << "Error: The buffer is NULL !" << endl;
		return -1;
	}

	mailFormated = heloStr +" Serveur !\r\n" +
		       mailFromStr + " <" + mail.getMailFrom() + ">\n" +
		       rcptToStr + " <" + mail.getMailTo() + ">\n" +
		       dataStr + "\r\n" +
		       mail.getBody() + "\r\n" +
		       ".\r\n" +
		       quitStr + "\r\n\r\n";

	if( strlen(mailFormated.c_str()) >= (unsigned int)bufferSize )
		cerr << "Warning: The buffer size is too small !" << endl;

	strcpy( buffer, mailFormated.c_str() );

	return 0;
}

string SmtpProtocol::formatMail(const Mail & mail)
{
	string	mailFormated;
	string	heloStr(HELO);
	string	mailFromStr(MAIL_FROM);
	string	rcptToStr(RCPT_TO);
	string	dataStr(DATA);
	string	quitStr(QUIT);

	mailFormated = heloStr +" Serveur !\r\n" +
		       mailFromStr + " <" + mail.getMailFrom() + ">\n" +
		       rcptToStr + " <" + mail.getMailTo() + ">\n" +
		       dataStr + "\r\n" +
		       mail.getBody() + "\r\n" +
		       ".\r\n" +
		       quitStr + "\r\n\r\n";

	return mailFormated;
}
